<template>

<el-container class="home-container">
    <!-- 头部 -->
    <el-header>
        <div>
            <span>后台管理系统</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体容器 -->
    <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
            <!-- 折叠效果 -->
            <div class="toggle-btn" @click="toggleCollapse">|||</div>
            <!-- 侧边栏菜单 -->
            
            <el-menu
                background-color="#606266"
                text-color="#fff"
                active-text-color="#409fff" unique-opened :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
                <!-- 一级菜单 -->
                <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
                    <template slot="title">
                    <i :class="icons[item.id]"></i>
                    <span>{{item.authName}}</span>
                    </template>
                    <!-- 二级菜单 -->
                    <el-menu-item :index="subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="navState(subItem.path)">
                        <!-- 二级菜单的模板 -->
                        <template slot="title">
                            <i class="el-icon-menu"></i>
                            <span>{{subItem.authName}}</span>
                        </template>
                    </el-menu-item>
                </el-submenu>
            </el-menu>
        </el-aside>
        <!-- 主体 -->
        <el-main>
            <!-- 这是一个路由点位符 -->
            <router-view></router-view>
        </el-main>
    </el-container>
</el-container>

</template>
<script>

export default {
    data() {
        return {
         //左侧菜单的数据
        menuList:[],
        icons:{
            '125':'iconfont icon-user',
            '103':'iconfont icon-tijikongjian',
            '101':'iconfont icon-shangpin',
            '102':'iconfont icon-danju',
            '145':'iconfont icon-baobiao',
            },
            //表示不折叠
            isCollapse:false,
            activePath:''
        }
    },
    created() {
        this.getMenuList()
        //从sessionStorage中获取到activePath并赋值给属性activePath
        this.activePath = window.sessionStorage.getItem('activePath')
       
    },
    methods: {
        handleOpen(key, keyPath) {
        console.log(key, keyPath);
      },
      handleClose(key, keyPath) {
        console.log(key, keyPath);
      }
      ,
        logout(){
            //清除sessionStorage中的数据(token)
            window.sessionStorage.clear();
            this.$router.push('/login')
        },
        async getMenuList(){
            //需要向后台发送axios请求获取数组
            const {data:res} = await this.$http.get('menus')
            //判断数据是否获取到了
            if(res.meta.status!==200) return this.$message.error(res.meta.msg)
            this.menuList = res.data
            console.log(res)
        },
        toggleCollapse(){
            //处理菜单的折叠效果
            this.isCollapse = !this.isCollapse
        },
        //链接的激活状态
        navState(path){
            window.sessionStorage.setItem('activePath',path)
        }

    },
}
</script>

<style lang="less" scoped>
    .home-container {
        height: 100%;
    }
    .el-header {
        background-color: #303133;
        display: flex;
        justify-content: space-between;
        padding-left: 0;
        align-items: center;
        color: #ffffff;
        font-size: 20px;
        > div {
            display: flex;
            align-items: center;
            span {
                margin-left: 15px;
            }
        }
    }
    .el-aside {
        background-color: #606266;
        .el-menu{
            border-right: none;
        }
    }
    .el-main {
        background-color: #DCDFE6;
    }
    .iconfont{
        margin-right: 12px;
    }

    .toggle-btn{
        background-color: #909399;
        font-size: 10px;
        color: #ffffff;
        letter-spacing: 2px;
        text-align: center;
        line-height: 22px;
        cursor: pointer;
    }
</style>

